home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / text / manipulation / cv.lha / cv / cvt / tfname.c < prev   
Encoding:
C/C++ Source or Header  |  1993-12-31  |  2.5 KB  |  131 lines

  1. /*
  2.  *  TFNAME.C
  3.  *
  4.  *  (c)Copyright 1992 by Tobias Ferber,  All Rights Reserved.
  5.  */
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. #ifndef MAXIMUM_PATHNAME_LENGTH
  13. #define MAXIMUM_PATHNAME_LENGTH 256L
  14. #endif
  15.  
  16. /*** / AMIGA DEFINES / ***/
  17.  
  18. #if defined(AMIGA)
  19. #define TEMP_PATH "T:"     /* pathname for temporary files */
  20. #ifndef PSLASH
  21. #define PSLASH '/'         /* concatenated to pathnames (if needed) */
  22. #endif /* !PSLASH */
  23.  
  24. #ifndef PSEP
  25. #define PSEP ';'           /* seperator for pathnames in environment vars */
  26. #endif /* !PSEP */
  27.  
  28. /*** / MS-DOOF DEFINES / ***/
  29.  
  30. #elif defined(__MSDOS__)
  31. #define TEMP_PATH getenv("TEMP")
  32.  
  33. #ifndef PSLASH
  34. #define PSLASH '\\'
  35. #endif /* !PSLASH */
  36.  
  37. #ifndef PSEP
  38. #define PSEP ';'
  39. #endif /* !PSEP */
  40.  
  41. /*** / UNIX DEFINES / ***/
  42.  
  43. #elif defined(unix)
  44. #define TEMP_PATH "/usr/tmp/"
  45. #ifndef PSLASH
  46. #define PSLASH '/'
  47. #endif /* !PSLASH */
  48.  
  49. #ifndef PSEP
  50. #define PSEP ';'
  51. #endif /* !PSEP */
  52.  
  53. #else /* unknown */
  54. #error "I don't know anything about pathnames on your machine!"
  55.  
  56. #endif /* temp const */
  57.  
  58. #define DEFAULT_TEMPFILE_FORMAT "temp%d.tmp"
  59.  
  60. /*
  61.  *
  62.  * FUNCTION
  63.  *
  64.  *   tfname -- build a temporary filename
  65.  *
  66.  * SYNOPSIS
  67.  *   #include "filecopy.h"
  68.  *
  69.  *   tfn= tfname(fmt);
  70.  *   char *tfn;
  71.  *   char *fmt;
  72.  *
  73.  * DESCRIPTION
  74.  *
  75.  *   This function tries to allocate MAXIMUM_PATHNAME_LENGTH chars for
  76.  *   a unique filename in the local area for temporary files.
  77.  *   Said filename will be a concatenation of the local TEMP_PATH,
  78.  *   a PSLASH (if needed) and given format string 'fmt' which should
  79.  *   contain one optional '%d' to hold a random number.
  80.  *   The allocated filename buffer will be returned; (char *)0L if sth.
  81.  *   went wrong.
  82.  *
  83.  * NOTE
  84.  *
  85.  *   - The non-existance of the returned filename will not be verified.
  86.  *   - It's up to the caller to free() the returned filename buffer after
  87.  *     he/she's done with it.
  88.  *
  89.  */
  90.  
  91. char *tfname(fmt)
  92. char *fmt;
  93. {
  94.   char *tf;
  95.  
  96.   if( tf= (char *)malloc(MAXIMUM_PATHNAME_LENGTH * sizeof(char)) )
  97.   {
  98.     char *tpath;
  99.     int n= 0;
  100.  
  101.     tpath= TEMP_PATH;
  102.  
  103. #if defined(AMIGA)
  104.     n= strlen(tpath);
  105.     strcpy(tf, tpath);
  106.  
  107. #elif defined(__MSDOS__)
  108.     if(tpath)
  109.     { char *t;
  110.       for(t= tpath, n=0; *t && *t!=PSEP; tf[n++]= *t++) ;
  111.  
  112.       if(tf[n]!=PSLASH)
  113.         tf[n++]= PSLASH;
  114.     }
  115.     else n= 0;
  116.  
  117. #endif /* MACHINES */
  118.  
  119.     if( !(fmt && *fmt) )
  120.       fmt= DEFAULT_TEMPFILE_FORMAT;
  121.  
  122.     { time_t t;
  123.       time(&t);
  124.       srand(t*270970*rand()+42);
  125.       sprintf(&tf[n],fmt,rand());
  126.     }
  127.   }
  128.  
  129.   return tf;
  130. }
  131.